package com.iss.card.view.mycard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.view.ContainerActivity;
import com.iss.card.view.model.CardDetailHistoryModel;
import com.iss.card.view.model.CardDetailBrowsetModel;

public class CardBagUtils {
	
	 public static final int CARD_TYPE_COUNT = 1;
	 public static final int CARD_TYPE_COMPOSITE = 2;
	 
	 public static final int CARD_ATTRIBUTE_INTEGRAL = 1;
	 public static final int CARD_ATTRIBUTE_PRICE = 2;
	 public static final int CARD_ATTRIBUTE_DISCOUNT = 3;
	 
	 /**
	  * ��ֿ�
	  */
	 static private final int CARD_TYPE_REMAIN_SCORE = 1;
	 /**
	  * ��ֵ��
	  */
	 static private final int CARD_TYPE_REMAIN_MONEY = 2;
	 /**
	  * �ۿۿ�
	  */
	 static private final int CARD_TYPE_DISCOUNT = 3;
	 /**
	  * �ƴο�
	  */
	 static private final int CARD_TYPE_REMAIN_TIME = 4;
	
	/**
	 * ��ӿ�����
	 * 
	 * @param activity
	 * @param view
	 * @param cardBagInfos
	 */
	public static void addCardContent(final Activity activity, LinearLayout layout, final CardBagModel info){
		
		
	
		LayoutInflater inflater = activity.getLayoutInflater();
		LinearLayout cardContent = (LinearLayout) inflater.inflate(R.layout.card_content, null);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		params.topMargin = 5;
		params.rightMargin = 3;
		cardContent.setLayoutParams(params);
//		TextView cardNameVal = (TextView) cardContent.findViewById(R.id.card_name_val);
//		cardNameVal.setText(info.cardName);
//		
//		TextView cardContentVal = (TextView) cardContent.findViewById(R.id.card_content_val);
//		cardContentVal.setText(info.remainMoney);
//		
		
		setCardCountBackground(cardContent,info);
		
		layout.addView(cardContent, params);
		
		
		cardContent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(" My card ", "�����ת!!");
				
				Intent intent = new Intent(activity, ContainerActivity.class);
				intent.putExtra(ApplicationManager.KEY_PUT_EXTRA_BY_MY_CARD, info);
				intent.putExtra(ApplicationManager.INTENT_KEY, ApplicationManager.INTENT_FRAGMENT_MY_CARD);
				activity.startActivity(intent);
			}
		});
	}

	/**
	 * ���type��attribute���ñ���ɫ.
	 * 
	 * @param cardContent ���ñ����Ĳ��ֿؼ�.
	 * @param info �õ�type,attribute�������.
	 */
	private static void setCardCountBackground(LinearLayout cardContent,
			CardBagModel info) {
//		RelativeLayout card_name_line = (RelativeLayout) cardContent
//				.findViewById(R.id.card_name_line);
//		RelativeLayout card_name_content_line = (RelativeLayout) cardContent
//				.findViewById(R.id.card_content_line);
//		final int cardType = Integer.parseInt(info.cardType);
//		String cardAttributes[] = info.cardAttribute.split(",");
//		switch (cardType) {
//		case CARD_TYPE_COUNT:
//			card_name_line.setBackgroundResource(R.drawable.count_background1);
//			card_name_content_line
//					.setBackgroundResource(R.drawable.count_background2);
//			break;
//
//		case CARD_TYPE_COMPOSITE:
//			if (cardAttributes.length > 2) {// �ۺ�
//				card_name_line
//						.setBackgroundResource(R.drawable.composite_background1);
//				card_name_content_line
//						.setBackgroundResource(R.drawable.composite_background2);
//				break;
//			}
//			final int cardAttribute = Integer.parseInt(cardAttributes[0]);
//			switch (cardAttribute) {
//			case CARD_ATTRIBUTE_INTEGRAL:
//				card_name_line
//						.setBackgroundResource(R.drawable.integral_background1);
//				card_name_content_line
//						.setBackgroundResource(R.drawable.integral_background2);
//				break;
//			case CARD_ATTRIBUTE_PRICE:
//				card_name_line
//						.setBackgroundResource(R.drawable.price_background1);
//				card_name_content_line
//						.setBackgroundResource(R.drawable.price_background2);
//				break;
//			case CARD_ATTRIBUTE_DISCOUNT:
//				card_name_line
//						.setBackgroundResource(R.drawable.discount_background1);
//				card_name_content_line
//						.setBackgroundResource(R.drawable.discount_background2);
//				break;
//			default:
//				Log.e("addCardContent",
//						"set card count background by attribute error!");
//				break;
//			}
//
//			break;
//		default:
//			Log.e("addCardContent", "set card count background by type error!");
//			break;
//		}

	}
	
	/**
	 * �����ַ�
	 * 
	 * @param str
	 * @return
	 */
	static private char doAnalysisString(String str) {
		String analysisStr = str.substring(0, str.indexOf(":"));
		if (analysisStr.equals("UseTimes")) {
			return 'U';
		} else if (analysisStr.equals("Discount")) {
			return 'D';
		} else if (analysisStr.equals("RemainScore")
				|| analysisStr.equals("UseScore")
				|| analysisStr.equals("RemainMoney")) {
			return 'R';
		} else {
			return '?';
		}
	}

	/**
	 * ���ַ�ָ�
	 * 
	 * @param expr
	 * @return
	 */
	static private String[] doAnalysisOnExpr(String str, String expr) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}

		return str.split(expr);
	}

	static final class VipInfoContent {
		char mark;

		String useTimes;
		String times;

		String discount;

		char type;
		String remainScore;
		String remainMoney;
		String useScore;
	}

	/**
	 * ����webService ����ֵ
	 * 
	 * @param str
	 * @param vipInfoContent
	 */
	public static ArrayList<VipInfoContent> analysisString(String str) {

		char c = doAnalysisString(str);
		ArrayList<VipInfoContent> contents = new ArrayList<CardBagUtils.VipInfoContent>();

		if (c == 'U') {

			String[] strPre = doAnalysisOnExpr(str, ",");
			List<String> strPres = Arrays.asList(strPre);
			for (String s : strPres) {

				String[] strFin = doAnalysisOnExpr(s, ":");

				VipInfoContent infoContent = new VipInfoContent();
				infoContent.mark = c;
				if ("UseTimes".equals(strFin[0])) {
					infoContent.useTimes = strFin[1];
				} else {
					infoContent.times = strFin[1];
				}
				contents.add(infoContent);
			}
		} else if (c == 'D') {

			String[] strPre = doAnalysisOnExpr(str, ":");
			VipInfoContent infoContent = new VipInfoContent();
			infoContent.mark = c;
			infoContent.discount = strPre[1];
			contents.add(infoContent);
		} else if (c == 'R') {

			String[] strPre = doAnalysisOnExpr(str, ";");
			List<String> strPres = Arrays.asList(strPre);
			for (String s : strPres) {

				String[] strFin = doAnalysisOnExpr(s, ":");

				VipInfoContent infoContent = new VipInfoContent();
				infoContent.mark = c;

				if ("RemainScore".equals(strFin[0])) {
					infoContent.remainScore = strFin[1];
					infoContent.type = 'S';
				} else if ("RemainMoney".equals(strFin[0])) {
					infoContent.remainMoney = strFin[1];
					infoContent.type = 'M';
				} else if ("UseScore".equals(strFin[0])) {
					infoContent.useScore = strFin[1];
					infoContent.type = 'U';
				}
				contents.add(infoContent);
			}
		} else {
			// no info
		}

		return contents;
	}
	
		
	/**
	 * �����������
	 * 
	 * @param info ��ݼ�
	 * @return HashMap<Integer, String>
	 */
	public static HashMap<Integer, String> analysisData(CardBagModel info){
		
		HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
		int cardType = Integer.parseInt(info.cardType);
		switch (cardType) {
		case 1: //�ƴο�
//			hashMap.put(CARD_TYPE_REMAIN_TIME, appendStrng(info.remainTimes, info.times));
			break;
		case 2: //�ۺϿ�
			addHashMapValues(cardType, hashMap, info);
			break;
		default:
			break;
		}
		return hashMap;
	}
	
	/**
	 * ����ۺϿ����
	 * 
	 * @param type ������
	 * @param hashMap ����
	 * @param info ��ݼ�
	 */
	static private void addHashMapValues(int type, HashMap<Integer, String> hashMap, CardBagModel info){
		switch (type) {
		case CARD_TYPE_REMAIN_SCORE:
//			hashMap.put(CARD_TYPE_REMAIN_SCORE, info.remainScore);
			break;
		case CARD_TYPE_REMAIN_MONEY:
//			hashMap.put(CARD_TYPE_REMAIN_MONEY, info.remainMoney);
			break;
		case CARD_TYPE_DISCOUNT:
//			hashMap.put(CARD_TYPE_DISCOUNT, info.discount);
			break;
		default:
			break;
		}
	}
	
	/**
	 * ��ݿ���Ϣ���View
	 * 
	 * @param context
	 * @param layout
	 * @param info
	 */
	public static void addVipInfo(Activity context, LinearLayout layout,  HashMap<Integer, String> hashMap) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.vip_card_info_view, null);
		layout.addView(view);

		TextView vipCardName = (TextView) view.findViewById(R.id.vip_card_name);
		TextView computeValues = (TextView) view.findViewById(R.id.compute_values);
		TextView valuesUnits = (TextView) view.findViewById(R.id.values_units);
		
		
		if(hashMap.get(CARD_TYPE_REMAIN_TIME) != null){
			
			setTextVales(vipCardName, computeValues, valuesUnits,
					R.string.remain_num, hashMap.get(CARD_TYPE_REMAIN_TIME), R.string.values_null);
		}else if(hashMap.get(CARD_TYPE_REMAIN_TIME) != null){
			
			setTextVales(vipCardName, computeValues, valuesUnits,
					R.string.vip_integral, hashMap.get(CARD_TYPE_REMAIN_TIME), R.string.integral_unit);
		}else if(hashMap.get(CARD_TYPE_REMAIN_TIME) != null){
			
			setTextVales(vipCardName, computeValues, valuesUnits,
					R.string.store_value_balance, hashMap.get(CARD_TYPE_REMAIN_TIME), R.string.store_value_unit);
		}else if(hashMap.get(CARD_TYPE_REMAIN_TIME) != null){
			
			setTextVales(vipCardName, computeValues, valuesUnits,
					R.string.vip_discounts, hashMap.get(CARD_TYPE_REMAIN_TIME), R.string.discounts_unit);
		}
	}
	
	static private void setTextVales(TextView vipCardNameTx, TextView computeValuesTx, TextView valuesUnitsTx,
			int vipCardName, String computeValues, int valuesUnits){
		vipCardNameTx.setText(vipCardName);
		computeValuesTx.setText(computeValues);
		valuesUnitsTx.setText(valuesUnits);
	}

	static private String appendStrng(String values, String unit) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(values);
		buffer.append("/");
		buffer.append(unit);
		return buffer.toString();
	}
	
	
	/**
	 * ���VIP����ʷ��¼ View
	 * 
	 * @param context
	 * @param layout
	 * @param historyTraceInfos
	 */
	public static void addCardHistoryTraceView(Activity context, LinearLayout layout, CardDetailHistoryModel historyTraceInfo){
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.vip_card_history, null);
		layout.addView(view);
		
		TextView userNameValues = (TextView) view.findViewById(R.id.user_name_values);
		TextView consumptionTimeValues = (TextView) view.findViewById(R.id.consumption_time_values);
		TextView consumptionMoneyValues = (TextView) view.findViewById(R.id.consumption_money_values);
		TextView storeCardBalanceValues = (TextView) view.findViewById(R.id.store_card_balance_values);
		TextView consumptionDiscountValues = (TextView) view.findViewById(R.id.consumption_discount_values);
		TextView consumptionTimesValues = (TextView) view.findViewById(R.id.consumption_times_values);
		TextView exchangeIntegralValues = (TextView) view.findViewById(R.id.exchange_integral_values);
		TextView vipCardIntegralValues = (TextView) view.findViewById(R.id.vip_card_integral_values);
		TextView consumptionStoreValues = (TextView) view.findViewById(R.id.consumption_store_values);
		
		userNameValues.setText(historyTraceInfo.userMobile);
		consumptionTimeValues.setText(historyTraceInfo.cDate);
		consumptionMoneyValues.setText(historyTraceInfo.saleMoney);
		storeCardBalanceValues.setText(historyTraceInfo.useMoney);
		consumptionDiscountValues.setText(historyTraceInfo.discount);
		consumptionTimesValues.setText(historyTraceInfo.amount);
		exchangeIntegralValues.setText(historyTraceInfo.useScore);
		vipCardIntegralValues.setText(historyTraceInfo.storeName);
		consumptionStoreValues.setText(historyTraceInfo.storeName);
		
	}
	
	/**
	 * ��ӿ������ĵ�����ϢView
	 * 
	 * @param context
	 * @param layout
	 * @param address
	 */
	public static void addStoreCardAss(Activity context, LinearLayout layout, CardDetailBrowsetModel assInfo){
		
		TextView addRessValues = new TextView(context);
		addRessValues.setText(doCombination(assInfo.provinceName, assInfo.cityName, assInfo.storeAddress));
		addRessValues.setBackgroundResource(R.drawable.my_card_bg1);
		addRessValues.setTypeface(Typeface.DEFAULT);
		addRessValues.setPadding(10, 0, 0, 0);
		addRessValues.setTextColor(Color.BLACK);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		addRessValues.setGravity(Gravity.CENTER_VERTICAL);
		addRessValues.setLayoutParams(layoutParams);
		
		layout.addView(addRessValues);
	}
	
	/**
	 * ƴ���ַ�
	 */
	static private String doCombination(String provinceName, String cityName, String storeAddress){
		StringBuffer buffer = new StringBuffer();
		buffer.append(provinceName);
		buffer.append(cityName);
		buffer.append(storeAddress);
		return buffer.toString();
	}
}
